//
//  CZSplitViewController.m
//  i0S007通用项目
//
//  Created by Apple on 16/2/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "CZSplitViewController.h"
#import "CZMenuViewController.h"
#import "CZDetailViewController.h"

@interface CZSplitViewController ()

@end

@implementation CZSplitViewController

- (instancetype)init {
    if (self = [super init]) {
        CZMenuViewController *master = [[CZMenuViewController alloc] init];

        // 设置master控制器
        [self addChildViewController:master];
        
        // 设置详情控制器
        CZDetailViewController *detail = [[CZDetailViewController alloc] init];
        [self addChildViewController:detail];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CommonBGColor;

    [self prepareSplitViewController];
}

/// 设置SplitViewController
- (void)prepareSplitViewController {
    // 默认情况iPad横屏会显示master 控制器,竖屏不会显示master控制器,需要用手从屏幕左边拖出来
    self.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
    
    [self setPrimaryColumnWidthOrientatin:[UIApplication sharedApplication].statusBarOrientation];
}

/**
 *  设置split的master控制器的宽度
 *
 *  @param orientation 屏幕方向
 */
- (void)setPrimaryColumnWidthOrientatin:(UIInterfaceOrientation)orientation {
    // 1.获取到是横屏还是竖屏
    BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
    
    // 2.根据横屏或竖屏设置不同的宽度
    if (isLandscape) {
        self.maximumPrimaryColumnWidth = 280;
    } else {
        self.maximumPrimaryColumnWidth = 60;
    }
    
    // 3.获取到master控制器
    CZMenuViewController *master = self.childViewControllers[0];
    
    // 判断master的类型是否真的是CZMenuViewController
    if (![master isKindOfClass:[CZMenuViewController class]]) {
        return;
    }
    
    [master updateSubView:isLandscape];
}

// 控制器的view所在的屏幕尺寸发生改变(sizeClass)
// previousTraitCollection变化之前的sizeClass信息
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    // 想获取到的是屏幕变化之后的sizeClass信息
//    CZLog(@"当前sizeClass信息:%@", self.traitCollection);
    
    // 隐藏master控制器的containerView
    CZMenuViewController *master = self.childViewControllers[0];
    
    // 判断master的类型是否真的是CZMenuViewController
    if (![master isKindOfClass:[CZMenuViewController class]]) {
        return;
    }
    
    // 判断屏幕的宽度是紧凑
    BOOL isShow = self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact;
    
    // 调用 CZMenuViewController 来隐藏容器视图
    [master showContainerView:isShow];
}
@end
